﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace MDIDemo.Modules
{
    public partial class NguoiDung : DevExpress.XtraEditors.XtraForm
    {
        string sErr = "";
        int idThem = 4;
        int idSua = 5;
        int idXoa = 6;
        void SetPermission()
        {
            if (this.Parent.Parent is frmMain)
            {
                frmMain main = (frmMain)this.Parent.Parent;
                btnMThem.Visible = btnThem.Visible = frmMain.IsPermis(idThem, main.CurrentPermis.PERMISSION);
                btnMSua.Visible = btnSua.Visible = frmMain.IsPermis(idSua, main.CurrentPermis.PERMISSION);
                btnMXoa.Visible = btnXoa.Visible = frmMain.IsPermis(idXoa, main.CurrentPermis.PERMISSION);
                btnMLuu.Visible = btnLuu.Visible = frmMain.IsPermis(idThem, main.CurrentPermis.PERMISSION) && frmMain.IsPermis(idSua, main.CurrentPermis.PERMISSION);
            }
        }
        public NguoiDung()
        {
            InitializeComponent();
        }


        private void RefreshFormGroup(string p)
        {
            txtMaNhom.Text = p;
            txtTenNhom.Text = p;
        }
        private DTO.POGInfo GetDataGroup()
        {
            return new DTO.POGInfo(txtMaNhom.Text, "POG", txtMaNhom.Text, txtTenNhom.Text, "", "", "");
        }
        private void EnabledFormGroup(bool p)
        {
            txtMaNhom.Focus();
            txtMaNhom.Properties.ReadOnly = !p;
            txtTenNhom.Properties.ReadOnly = !p;
            btnXoa.Enabled = !p;
            btnThem.Enabled = !p;
            btnLuu.Enabled = p;
            btnXoa.Enabled = !p;
            btnSua.Enabled = !p;
            btnBoQua.Enabled = p;
            gridUser.Enabled = !p;
            gpUser.Enabled = !p;

        }
        private void SetDataGroup(DTO.POGInfo inf)
        {
            txtMaNhom.Text = inf.ROLE_ID;
            txtTenNhom.Text = inf.ROLE_NAME;
            BUS.PODControl ctr = new BUS.PODControl();
            gridUser.DataSource = ctr.GetAll(inf.ROLE_ID, ref sErr);
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            RefreshFormGroup("");
            EnabledFormGroup(true);
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            EnabledFormGroup(true);
            txtMaNhom.Properties.ReadOnly = true;
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            RefreshFormGroup("");
            EnabledFormGroup(false);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            sErr = "";
            int[] currentRows = gvGroup.GetSelectedRows();
            if (currentRows != null && currentRows.Length > 0)
            {
                BUS.POGControl ctr = new BUS.POGControl();
                DTO.POGInfo inf = new DTO.POGInfo(gvGroup.GetDataRow(currentRows[0]));

                if (XtraMessageBox.Show("Bạn có thực sự muốn xóa", "Thông báo", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    sErr = ctr.Delete(inf.ROLE_ID);
                    if (sErr == "")
                        XtraMessageBox.Show("Xóa thành công", "Thông báo");
                    else XtraMessageBox.Show(sErr, "Lỗi");
                    gridGroup.DataSource = ctr.GetAll(ref sErr);
                }

            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            sErr = "";
            if (txtMaNhom.Text == "")
            {
                XtraMessageBox.Show("Vui lòng nhập mã nhóm!");
                return;
            }
            DTO.POGInfo inf = GetDataGroup();
            BUS.POGControl ctr = new BUS.POGControl();
            sErr = ctr.InsertUpdate(inf);
            if (sErr == "")
                XtraMessageBox.Show("Lưu thành công", "Thông báo");
            else XtraMessageBox.Show(sErr, "Lỗi");
            gridGroup.DataSource = ctr.GetAll(ref sErr);
            EnabledFormGroup(false);
        }


        private void gvGroup_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gvGroup.FocusedRowHandle > -1)
            {
                RefreshFormUser("");
                DTO.POGInfo inf = new DTO.POGInfo(gvGroup.GetFocusedDataRow());
                SetDataGroup(inf);
                gvUser_FocusedRowChanged(null, null);
            }
        }

        private void gridGroup_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
            {
                if (btnSua.Enabled)
                    btnSua_Click(null, null);
            }
            else if (e.KeyCode == Keys.N && e.Modifiers == Keys.Control)
            {
                if (btnThem.Enabled)
                    btnThem_Click(null, null);
            }
            else if (e.KeyCode == Keys.X && e.Modifiers == Keys.Control)
            {
                if (btnXoa.Enabled)
                    btnXoa_Click(null, null);
            }
        }
        private void RefreshFormUser(string p)
        {
            txtUserID.Text = p;
            txtUserName.Text = p;
            txtPassword.Text = p;
        }
        public static string HashString(string p)
        {
            return Convert.ToBase64String(new System.Security.Cryptography.SHA1CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(p)));
        }
        private DTO.PODInfo GetDataUser()
        {
            int[] currentRows = gvGroup.GetSelectedRows();
            if (currentRows != null && currentRows.Length > 0)
            {
                BUS.POGControl ctr = new BUS.POGControl();
                DTO.POGInfo inf = new DTO.POGInfo(gvGroup.GetDataRow(currentRows[0]));
                string pass = txtHash.Text;
                if (txtPassword.Text != "00000")
                    pass = HashString(txtPassword.Text);
                return new DTO.PODInfo(txtUserID.Text, "POD", txtUserID.Text, txtUserName.Text, "", "", inf.ROLE_ID, pass, Convert.ToString(btnRelease.Tag));
            }
            return null;

        }
        private void EnabledFormUser(bool p)
        {
            txtUserID.Focus();
            txtUserID.Properties.ReadOnly = !p;
            txtUserName.Properties.ReadOnly = !p;
            txtPassword.Properties.ReadOnly = !p;

            btnMXoa.Enabled = !p;
            btnMThem.Enabled = !p;
            btnMLuu.Enabled = p;
            btnMXoa.Enabled = !p;
            btnMSua.Enabled = !p;
            btnMBoQua.Enabled = p;
            gridUser.Enabled = !p;
            gpGroup.Enabled = !p;

        }
        private void SetDataUser(DTO.PODInfo inf)
        {
            txtUserID.Text = inf.USER_ID;
            txtUserName.Text = inf.USER_NAME;
            txtPassword.Text = "00000";
            txtHash.Text = inf.PASS;
            btnRelease.Tag = inf.EMP_ID;
            btnRelease.Enabled = inf.EMP_ID != "";
        }

        private void NguoiDung_Load(object sender, EventArgs e)
        {
            EnabledFormGroup(false);
            EnabledFormUser(false);
            BUS.POGControl ctr = new BUS.POGControl();
            gridGroup.DataSource = ctr.GetAll(ref sErr);
            SetPermission();
        }

        private void btnMThem_Click(object sender, EventArgs e)
        {
            RefreshFormUser("");
            EnabledFormUser(true);
        }

        private void btnMSua_Click(object sender, EventArgs e)
        {
            EnabledFormUser(true);
            txtUserID.Properties.ReadOnly = true;
        }

        private void btnMLuu_Click(object sender, EventArgs e)
        {
            sErr = "";
            if (txtMaNhom.Text == "")
            {
                XtraMessageBox.Show("Vui lòng chọn lại nhóm người dùng!");
                return;
            }
            DTO.PODInfo inf = GetDataUser();
            BUS.PODControl ctr = new BUS.PODControl();
            sErr = ctr.InsertUpdate(inf);
            if (sErr == "")
                XtraMessageBox.Show("Lưu thành công", "Thông báo");
            else XtraMessageBox.Show(sErr, "Lỗi");
            gvGroup_FocusedRowChanged(null, null);
            EnabledFormUser(false);
        }

        private void btnMBoQua_Click(object sender, EventArgs e)
        {
            RefreshFormUser("");
            EnabledFormUser(false);
        }

        private void btnMXoa_Click(object sender, EventArgs e)
        {
            sErr = "";
            int[] currentRows = gvUser.GetSelectedRows();
            if (currentRows != null && currentRows.Length > 0)
            {
                BUS.PODControl ctr = new BUS.PODControl();
                DTO.PODInfo inf = new DTO.PODInfo(gvUser.GetDataRow(currentRows[0]));

                if (XtraMessageBox.Show("Bạn có thực sự muốn xóa", "Thông báo", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    sErr = ctr.Delete(inf.USER_ID);
                    if (sErr == "")
                        XtraMessageBox.Show("Xóa thành công", "Thông báo");
                    else XtraMessageBox.Show(sErr, "Lỗi");
                    gvGroup_FocusedRowChanged(null, null);
                }

            }

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void NguoiDung_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                if (btnBoQua.Enabled)
                    btnBoQua_Click(null, null);
                if (btnMBoQua.Enabled)
                    btnMBoQua_Click(null, null);
            }
            else if (e.KeyCode == Keys.F7)
            {
                if (btnLuu.Enabled)
                    btnLuu_Click(null, null);
                if (btnMLuu.Enabled)
                    btnMLuu_Click(null, null);
            }

        }



        private void gridUser_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
            {
                if (btnMSua.Enabled)
                    btnMSua_Click(null, null);
            }
            else if (e.KeyCode == Keys.N && e.Modifiers == Keys.Control)
            {
                if (btnMThem.Enabled)
                    btnMThem_Click(null, null);
            }
            else if (e.KeyCode == Keys.X && e.Modifiers == Keys.Control)
            {
                if (btnMXoa.Enabled)
                    btnMXoa_Click(null, null);
            }
        }

        private void gvUser_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gvUser.FocusedRowHandle != -1)
            {
                DTO.PODInfo inf = new DTO.PODInfo(gvUser.GetFocusedDataRow());
                SetDataUser(inf);
            }
            else btnRelease.Enabled = false;
        }

        private void gvUser_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            if (gvUser.FocusedRowHandle != -1)
            {
                DTO.PODInfo inf = new DTO.PODInfo(gvUser.GetFocusedDataRow());
                SetDataUser(inf);
            }
        }

        private void btnRelease_Click(object sender, EventArgs e)
        {
            BUS.PODControl ctr = new BUS.PODControl();
            DTO.PODInfo inf = ctr.Get(txtUserID.Text, ref sErr);
            inf.EMP_ID = "";
            ctr.Update(inf);
            btnRelease.Enabled = false;
        }
    }
}